/**
 * author: wangdan
 */

/**
document.write("<script src='http://9.123.237.133:9080/GPRMS/script/dojo_lib/dojo/dojo.js' djConfig='isDebug:false, debugAtAllCosts: false, parseOnLoad: true'></script>");

includeCSS("http://9.123.237.133:9080/GPRMS/script/dojo_lib/dojo/resources/dojo.css");
includeCSS("http://9.123.237.133:9080/GPRMS/script/dojo_lib/dojox/grid/_grid/tundraGrid.css");



function includeCSS(p_file) {
	var v_css  = document.createElement('link');
	v_css.rel = 'stylesheet'
	v_css.type = 'text/css';
	v_css.href = p_file;
	document.getElementsByTagName('head')[0].appendChild(v_css);
}
**/

var data0 = {
	items: []
};

var allDeliverables = {
	items: []
};

var activeId;
var grid_0=null;
var selectedItem_0=null;

var allBrands = "Rational,Information Management,WebSphere,Lotus,Tivoli";

function Deliverables(node) {
	
	this.parentNode = node;
	
	this.show = function(){
	
		var datawrapper = {};
		datawrapper[Constant.POST_TYPE] = Constant.Action.Load;
		//datawrapper["IPD_PHASE"]=dojo.byId("filter").value;
		//datawrapper["BRAND"]=Brand.brandStr;

        var kw = {
            url: Constant.POST_URL,
            handleAs: "json",
            content: Util.wrapPostData("com.ibm.gbms.formbean.deliverable.DeliverableFormBean", "com.ibm.gbms.bp.deliverable.BPDeliverables", datawrapper),
            preventCache: true,
            load: dojo.hitch(this, this._show)
        };
        dojo.xhrPost(kw);
		
		
	};
	
	this._show = function(json){
		dojo.addOnLoad(dojo.hitch(this, function(){		
		
				var formatMyDate = function(value, rowIndex) {
					if(value != 0){
						return dojo.date.locale.format(new Date(value), {datePattern:"dd MMM yyyy", selector:"date", locale: window.userlocale});
					}else{
						return "";
					}
					
				};
 
 				/**
				 var typeMap = {
				      "Date": {
				          type: Date,
				          deserialize: function(value){
						  	  return new Date(value);
				          }
				       }
				 };
				 **/
			
				var layout = [
					{name: '&nbsp;', field: 'checkbox', width:'5%', styles:'text-align:center;'},
					{name: 'Name', field: 'name', width:'17%'},
					{name: 'Version', field: 'version', width:'8%'},
					{name: 'eGA', field: 'ega', width:'9%', formatter:formatMyDate},
					{name: 'Type', field: 'type', width: '7%'},
					{name: 'Division', field: 'division', width:'11%'},
					{name: 'IPD phase', field: 'ipdphase', width:'11%'},
					{name: 'Brand', field: 'brand', width:'10%'},
					{name: 'IPMT name', field: 'ipmtname', width:'11%'},
					{name: 'PDT name', field: 'pdtname', width:'11%'}					
				];
				var pc=json.deliverables;
				activeId=json.activeId.toString();
				
				for(var i = 0; i < pc.length; i ++){
					var data = pc[i];
					
					//if(data.ega != null && data.ega != ""){
					//	dateStr = dojo.date.locale.format(new Date(data.ega),{datePattern:'dd MMM yyyy', selector: "date", locale: window.userlocale});
					//}else{
					//	dateStr = "";
					//}
					
					var rowItem = {checkbox:"<input type='radio' name='deliverableRadioButton' id='"+data.id+"' value='"+data.id+"' onclick='Deliverable.onRadioSelected(\""+data.id+"\")'/>", 
												   name: data.name, 
												   version: data.version,
												   ega: data.ega,
												   type: data.type,
												   division: data.division,
												   ipdphase: data.ipdphase,
												   brand: data.brand,
												   ipmtname: data.ipmtname,
												   pdtname: data.pdtname,
												   pid:data.id};
					
					allDeliverables.items.push(rowItem);
				}
				var filter = json.IPD_PHASE;
				var brandtext = json.BRAND;
				if(!filter) filter="Non-GA Products and Components";
				if(!brandtext) brandtext=window.brands.substr(1,window.brands.length-2);
				dijit.byId('filter').attr("value", filter);
				dojo.byId("txt_brand").value=brandtext;
				Brand.brandStr=brandtext;
				Brand.init();
				dijit.byId('filter').onChange=Deliverable.filter;
				var filterData = {
					items: []
				};
				for(var i = 0; i < allDeliverables.items.length; i ++){
					var item = allDeliverables.items[i];
					if(filter == 'All Products and Components'){
						if(Brand.brandFilter(brandtext,item))
							filterData.items.push(item);
					}else if(filter == 'Non-GA Products and Components' && item.ipdphase != 'LifeCycle'){
						if(Brand.brandFilter(brandtext,item))
							filterData.items.push(item);
					}else if(filter == 'GA Products and Components' && item.ipdphase == 'LifeCycle'){
						if(Brand.brandFilter(brandtext,item))
							filterData.items.push(item);
					}
				};
				
				var deliverablesStore = new dojo.data.ItemFileWriteStore({data: filterData});
				
				deliverablesStore.comparatorMap = {};
				//deliverablesStore.comparatorMap["ega"] = Deliverable.dateSort;
				deliverablesStore.comparatorMap["name"]=function(a, b){
					a=a.toLowerCase(); b=b.toLowerCase(); return a.localeCompare(b);
				};
				
				//Key events
				var onKeyEvent=function(e){
					if(e.altKey || e.metaKey){
						return;
					}
					var dk = dojo.keys;
					switch(e.keyCode){
						case dk.ENTER:
						case dk.SPACE:
							if(this.focus.cell.index==0){
								dojo.query("input[type='radio']", this.focus.cell.getNode(this.focus.rowIndex))[0].checked=true;
								this.selection.clickSelect(this.focus.rowIndex,false,false);
								selectedItem_0=this.getItem(this.focus.rowIndex);
							}
							break;
						case dk.TAB:
							this.focus[e.shiftKey ? 'previousKey' : 'nextKey'](e);
							break;
						case dk.LEFT_ARROW:
						case dk.RIGHT_ARROW:
							if(!this.edit.isEditing()){
								dojo.stopEvent(e);
								var offset = (e.keyCode == dk.LEFT_ARROW) ? 1 : -1;
								if(dojo._isBodyLtr()){ offset *= -1; }
								this.focus.move(0, offset);
							}
							break;
						case dk.UP_ARROW:
							if(!this.edit.isEditing() && this.focus.rowIndex != 0){
								dojo.stopEvent(e);
								this.focus.move(-1, 0);
							}
							if(this.selection.getSelected().length!=0)
								dojo.byId(this.selection.getSelected()[0].pid[0]).checked=true;
							break;
						case dk.DOWN_ARROW:
							if(!this.edit.isEditing() && this.store && this.focus.rowIndex+1 != this.rowCount){
								dojo.stopEvent(e);
								this.focus.move(1, 0);
							}
							if(this.selection.getSelected().length!=0)
								dojo.byId(this.selection.getSelected()[0].pid[0]).checked=true;
							break;
						case dk.PAGE_UP:
							if(!this.edit.isEditing() && this.focus.rowIndex != 0){
								dojo.stopEvent(e);
								if(this.focus.rowIndex != this.scroller.firstVisibleRow+1){
									this.focus.move(this.scroller.firstVisibleRow-this.focus.rowIndex, 0);
								}else{
									this.setScrollTop(this.scroller.findScrollTop(this.focus.rowIndex-1));
									this.focus.move(this.scroller.firstVisibleRow-this.scroller.lastVisibleRow+1, 0);
								}
							}
							break;
						case dk.PAGE_DOWN:
							if(!this.edit.isEditing() && this.focus.rowIndex+1 != this.rowCount){
								dojo.stopEvent(e);
								if(this.focus.rowIndex != this.scroller.lastVisibleRow-1){
									this.focus.move(this.scroller.lastVisibleRow-this.focus.rowIndex-1, 0);
								}else{
									this.setScrollTop(this.scroller.findScrollTop(this.focus.rowIndex+1));
									this.focus.move(this.scroller.lastVisibleRow-this.scroller.firstVisibleRow-1, 0);
								}
							}
							break;
					}
				};
				grid_0 = new dojox.grid.DataGrid({
					id: "grid_0",
					autoHeight:10,
					//rowsPerPage:1000,
					store: deliverablesStore,
					structure: layout,
					selectionMode: "single",
					selectable:true,
					onRowClick: function(e){},
					onKeyDown: onKeyEvent
				}, document.createElement('div'));
				this.parentNode.appendChild(grid_0.domNode);
				
				//grid.onSelected="function(){}";
				//grid.onCellClick="function(){}";
				
				//The first column can not sort
				grid_0.canSort = function(index) {  
				  if(Math.abs(index) == 1) return false;
				  return true;
				}
				
				//Remove the sort icon for first column

				grid_0.startup();
				//grid_0.autoHeight=10;
				var hideSortIcon = function() {
				var thElements = dojo.query("th", dojo.byId('grid_0'));
				for(var i = 0; i < thElements.length; i ++){
					var th = thElements[i];
					if(th.cellIndex == 0){
						th.childNodes[0].className = "";
						}
					}
				};
				//dojo.connect(dijit.byId('grid_0'), "render", hideSortIcon);
				dojo.connect(dijit.byId('grid_0'), "update", hideSortIcon);
				grid_0.update();
				

				//tooltip
				var showTooltip = function(e){
					//name, IPMT name, PDT name				 
	                if (e.cell.index == 1 || e.cell.index == 8 || e.cell.index == 9) {
						var msg = e.cellNode.firstChild.data;
	                    dijit.showTooltip(msg, e.cellNode);
	                }
	            };
	            var hideTooltip = function(e){
	                dijit.hideTooltip(e.cellNode);
	                // FIXME: make sure that pesky tooltip doesn't reappear!
	                // would be nice if there were a way to hide tooltip without regard to aroundNode.
	                dijit._masterTT._onDeck = null;
	            }
	            
           	 	dojo.connect(dijit.byId('grid_0'), "onCellMouseOver", showTooltip);
				dojo.connect(dijit.byId('grid_0'), "onCellMouseOut", hideTooltip);
				
				var render_0=function (){
					if(!selectedItem_0) return;
					grid_0.selection.deselect(selectedItem_0);
					if(dojo.byId(selectedItem_0.pid[0])){
						grid_0.selection.addToSelection(selectedItem_0);
						dojo.byId(selectedItem_0.pid[0]).checked=true;
					}
				};
				dojo.connect(grid_0.scroller, "needPage",  render_0);
				//hide sort icon for first column
				
				
				//add loading indicate when sorting
				
				dojo.connect(dijit.byId('grid_0'), "sort", function(){Util.showLoader();});
				dojo.connect(dijit.byId('grid_0'), "update", function(){Util.hideLoader();});
				
				
				Util.hideLoader();	
			}));
	};
}

var Deliverable = {
	///////////shu_ecust/////////////////
	cRowCount: function(arg0){
		if(arg0=="All")
			arg0=grid_0.rowCount;
		if((arg0-0)>grid_0.rowCount)
			grid_0.autoHeight=grid_0.rowCount;
		else
			grid_0.autoHeight=arg0-0;
		grid_0._autoHeight=false;
		grid_0.update();
		setTimeout(function(){
			grid_0.update()
		},10);
	},
	_onRadioSelected:function(items, request){
		if(selectedItem_0)
			grid_0.selection.deselect(selectedItem_0);
		selectedItem_0=items[0];
		grid_0.selection.addToSelection(selectedItem_0);
		dojo.byId(selectedItem_0.pid[0]).checked=true;
	},
	onRadioSelected: function(id){
		grid_0.store.fetch({query: {pid:id}, onComplete: this._onRadioSelected});
		GPAManager.list.submit(id);
		//selectedItem_0=grid_0.getItem(e.rowIndex);
		dojo.byId("viewbutton").className = "button-blue";
		dojo.byId("view_pc").disabled = false;
		dojo.byId("createbutton").className = "button-blue";
		dojo.byId("create_plan").disabled = false;
	},
	filter : function(){
		Util.showLoader();
		var filter = dojo.byId("filter").value;
		var brandtext = dijit.byId("txt_brand").attr('value');
		
		var grid = dijit.byId("grid_0");
		
		var filterData = {
			items: []
		};
		
		for(var i = 0; i < allDeliverables.items.length; i ++){
			var item = allDeliverables.items[i];
			
			/*if(item.pid=="1150102371359")
				console.log(item.name+"---"+item.ipdphase);*/
			if(filter == 'All Products and Components'){
				if(Brand.brandFilter(brandtext,item))
					filterData.items.push(item);
			}else if(filter == 'Non-GA Products and Components' && item.ipdphase != 'LifeCycle'){
				if(Brand.brandFilter(brandtext,item))
					filterData.items.push(item);
			}else if(filter == 'GA Products and Components' && item.ipdphase == 'LifeCycle'){
				if(Brand.brandFilter(brandtext,item))
					filterData.items.push(item);
			}

			/*if(Brand.brandFilter(brandtext,item.brand)){
				if(filter == 'All Products and Components'){
					filterData.items.push(item);
				}else{
					if(filter == 'Non-GA Products and Components' && item.ipdphase != 'LifeCycle'){
						filterData.items.push(item);
						
					}else if(filter == 'GA Products and Components' && item.ipdphase == 'LifeCycle'){
						filterData.items.push(item);
					}
				}				
			}*/
		}
		
		var filterStore = new dojo.data.ItemFileWriteStore({data: filterData});
		filterStore.comparatorMap = {};
		filterStore.comparatorMap["ega"] = Deliverable.dateSort;
		filterStore.comparatorMap["name"]=function(a, b){
			a=a.toLowerCase(); b=b.toLowerCase(); return a.localeCompare(b);
		};
		
		var idOfSelected=null;
		if(selectedItem_0){
			grid_0.selection.deselect(selectedItem_0);
			idOfSelected=selectedItem_0.pid[0];
			selectedItem_0=null;
		}
    	dijit.byId("grid_0").setStore(filterStore);
		Deliverable.cRowCount(dijit.byId("rowCount").attr("value"));
		dijit.byId("grid_0").update();
		var gotItems = function(items, request){
			if (items.length > 0) {
				grid_0.selection.addToSelection(items[0]);
				selectedItem_0=items[0];
				if(dojo.byId(selectedItem_0.pid[0])) dojo.byId(selectedItem_0.pid[0]).checked=true;
			}else{
				//reset the view button
				dojo.byId("viewbutton").className="button-gray";
				dojo.byId("view_pc").disabled = true;
				dojo.byId("createbutton").className="button-gray";
				dojo.byId("create_plan").disabled = true;
				GPAManager.list.submit();
			}	
		};
		if (idOfSelected) {
			filterStore.fetch({query: {pid: idOfSelected}, onComplete: gotItems});
		}
		var datawrapper = {};
		datawrapper[Constant.POST_TYPE] = "SET_IPD_BRAND";
		datawrapper["IPD_PHASE"]=filter;
		datawrapper["BRAND"]=brandtext;
        var kw = {
            url: Constant.POST_URL,
            handleAs: "json",
            content: Util.wrapPostData("com.ibm.gbms.formbean.deliverable.DeliverableFormBean", "com.ibm.gbms.bp.deliverable.BPDeliverables", datawrapper),
            preventCache: true
            //load: dojo.hitch(this, this._show)
        };
        dojo.xhrPost(kw);
		//dojo.cookie("BRAND", brandtext);
		//dojo.cookie("IPD_PHASE", filter);
				
		Util.hideLoader();
	},
	
	dateSort: function(a, b){
		var ret = 0;
		if(a == "" && b != ""){
			ret = -1;
		}else if(a != "" && b == ""){
			ret = 1;
		}else if(a != "" && b != ""){
			var date1 = dojo.date.locale.parse(a,{datePattern:'dd MMM yyyy', selector: "date", locale: window.userlocale});
			var date2 = dojo.date.locale.parse(b,{datePattern:'dd MMM yyyy', selector: "date", locale: window.userlocale});
																	
			ret = dojo.date.compare(date1,date2,"date");
		}
		return ret;
	},
	
	getNameVersion:function(pid){
		for(var i=0;i<allDeliverables.items.length;i++){
			var item = allDeliverables.items[i];
			if(item.pid == pid){
				return item.name+";"+item.version;
			}
		}
		
	},
	
	showDetailData: function(){
		/*var id = null;
		var radios = document.getElementsByName('deliverableRadioButton');
		for (i = 0; i < radios.length; i++) {
			if (radios[i].checked == true) {
				id = radios[i].value;
			}
		}*/
		var id=selectedItem_0.pid[0];
		if(id == null){
			Util.showErrorMsg('Please select one deliverable!');
		}else{
			var datawrapper = {};
			datawrapper[Constant.POST_TYPE] = Constant.Action.View;
			datawrapper["id"] = id;
	
	        var kw = {
	            url: Constant.POST_URL,
	            handleAs: "json",
	            content: Util.wrapPostData("com.ibm.gbms.formbean.deliverable.DeliverableFormBean", "com.ibm.gbms.bp.deliverable.BPDeliverables", datawrapper),
	            preventCache: true,
	            load: dojo.hitch(this, this._showDetailData)
	        };
	        dojo.xhrPost(kw);
			
			Util.showLoader();
		}
		
	}, 
	
	_showDetailData: function(json) {
		Util.hideLoader();
		
		//General information
		dojo.byId("name").innerHTML = json.name;
		dojo.byId("version").innerHTML = json.version;
		dojo.byId("shortName").innerHTML = json.shortName;
		dojo.byId("type").innerHTML = json.type;
		
		//Key information
		this.setDate('division', json.division);
		this.setDate('ipmtName', json.ipmtName);
		this.setDate('divisionGroup', json.divisionGroup);
		this.setDate('pdtName', json.pdtName);
		this.setDate('ipdPhase', json.ipdPhase);
		this.setDate('codeName', json.codeName);
		this.setDate('brand', json.brand);
		this.setDate('clientServer', json.clientServer);
		this.setDate('platform', json.platform);
		
		// Contact
		this.setDate('owner', json.owner);
		this.setDate('devContact', json.devContact);
		this.setDate('marketContact', json.marketContact);
		this.setDate('pdtLeader', json.pdtLeader);
		this.setDate('devVP', json.devVP);
		
		// Schedule
		this.setDate('overallStatus', json.overallStatus);
		this.setDate('planRisk', json.planRisk);
		
		var keyDates = {
			items: []
		};
		
		keyDates.items.push(this.getKeyDateItem(json.dcp));
		keyDates.items.push(this.getKeyDateItem(json.ann));
		keyDates.items.push(this.getKeyDateItem(json.ga));
		keyDates.items.push(this.getKeyDateItem(json.annWithdrawl));
		keyDates.items.push(this.getKeyDateItem(json.effWithdrawl));

		var keyDateStore = new dojo.data.ItemFileWriteStore({data: keyDates});
		
		//hide sort icon and disable sort
		var hideSortIcon = function() {
		var thElements = dojo.query("th", dojo.byId('grid_3'));
		for(var i = 0; i < thElements.length; i ++){
				var th = thElements[i];
				th.childNodes[0].className = "";
			}
		};
			
		dojo.connect(dijit.byId('grid_3'), "render", hideSortIcon);
		dojo.connect(dijit.byId('grid_3'), "update", hideSortIcon);
		
		dijit.byId('grid_3').canSort = function(index) {  
			return false;
		};
		
		dijit.byId('grid_3').setStore(keyDateStore);
		dijit.byId('grid_3').autoHeight = 5;
		dijit.byId('grid_3').update();
		/*setTimeout(function(){
			dijit.byId('grid_3').update();
		},10);*/
		//dojo.connect(dijit.byId('schedule'),'onShow',this.fixGridRender);

		//dependency
		var dependencies = json.dependencies;
		dojo.byId('dependencyTitle').innerHTML = json.name + " depands on:"
		var dependencyDiv = dojo.byId('dependency_list');
		dependencyDiv.innerHTML = "";
		var htmlStr = "<ul>";
		for(var i = 0; i < dependencies.length; i ++){
			htmlStr += "<li>" + dependencies[i].deliverableName + "</li>";
		}
		htmlStr += "</ul>";
		dependencyDiv.innerHTML = htmlStr;
		
		if(dependencies.length == 0){
			dependencyDiv.innerHTML = "There is no dependency.";
		}
		
		/*dijit.byId('generalInfo').selected = "true";
		dijit.byId('keyInfo').selected = "false";
		dijit.byId('contact').selected = "false";
		dijit.byId('schedule').selected = "false";
		dijit.byId('dependency').selected = "false";
		dijit.byId('history').selected = "false";*/
		//Make sure the "generalInfo" tab is selected.
		dijit.byId('tabcontainer').selectChild(dijit.byId('generalInfo')); 
		deliverableData.show();		
	},
	
	getKeyDateItem: function(keydate){
		return {keyDates:keydate.keyDate,
					   original:this.formatDate(keydate.original), 
					   currentPlan: this.formatDate(keydate.currentPlan), 
					   currentExpectation: this.formatDate(keydate.currentExpectation), 
					   actual: this.formatDate(keydate.actual)};
	},
	
	formatDate: function(date){
		var dateStr = "";
		if (date != null && date != "") {
			dateStr = dojo.date.locale.format(new Date(date), {
				datePattern: 'dd MMM yyyy',
				selector: "date",
				locale: window.userlocale
			});
		}
		return dateStr;
	},
	
	setDate: function(id, value){
		dojo.byId(id).innerHTML = value;
	},	


	fixGridRender : function(){
		setTimeout(function(){
			dijit.byId('grid_3').update();
		},200);
	}
   
};

var Brand = {
	brandStr:"",
	init:function(){
		this.initBrand();
		this.fn_addHandlers();
	},
	
	hideTTDlg:function (evt){
		if(evt.keyCode==dojo.keys.ESCAPE){
			dijit.popup.close(dijit.byId("dlg_brand"));
			this.hideBrandDlg(dijit.byId("dlg_brand"));
		}
	},


	fn_addHandlers:function(){
		//dojo.connect(dijit.byId('schedule'),'onShow',fcn1);
		//dojo.connect(dijit.byId('grid_0'),'onCellClick',fcn2);
		//dojo.connect(dijit.byId('grid_1'),'onRowClick',dijit.byId('grid_1'),fcn3);
		//dojo.connect(dijit.byId('grid_1'),'onSelected',	dijit.byId('grid_1'),fcn4);
		//dojo.connect(dijit.byId('grid_1'),'onDeselected',dijit.byId('grid_1'),fcn5);
		dojo.connect(dojo.byId('dlg_brand'),'onkeypress',Brand.hideTTDlg);
		//dojo.connect(dijit.byId('grid_1'),'onApplyCellEdit',fixStartEdit);
		//tooltip
			var showTooltip = function(e){				 
                if (e.cell.index == 1) {
										var msg = e.cellNode.firstChild.data;
                    dijit.showTooltip(msg, e.cellNode);
                }
            };
            var hideTooltip = function(e){
                dijit.hideTooltip(e.cellNode);
                // FIXME: make sure that pesky tooltip doesn't reappear!
                // would be nice if there were a way to hide tooltip without regard to aroundNode.
                dijit._masterTT._onDeck = null;
            }
            
            dojo.connect(dijit.byId('grid_0'), "onCellMouseOver", showTooltip);
						dojo.connect(dijit.byId('grid_0'), "onCellMouseOut",Brand.hideTooltip);
            dojo.connect(dijit.byId('grid_1'), "onCellMouseOver", showTooltip);
						dojo.connect(dijit.byId('grid_1'), "onCellMouseOut",Brand.hideTooltip);
		//for grid_0 radiobutton selection
			dojo.connect(dijit.byId('grid_0'), "onCellClick", function(e){
				if(e.cellIndex==0){
					if(dojo.query("input",e.cellNode)[0].checked){
						e.grid.updateRowStyles(e.rowIndex);
						dojo.byId("viewbutton").className="button-blue";
						dojo.byId("view_pc").disabled = false;
					}
				}
			});
			dojo.connect(dijit.byId('grid_1'), "onCellClick", function(e){
				if(e.cellIndex==0){
					/*if(dojo.query("input",e.cellNode)[0].checked)
						e.rowNode.className="dojoxGridRowSelected";
					else{
						if(e.rowIndex%2==0)
							e.rowNode.className="dojoxGridRowEven";
						else
							e.rowNode.className="dojoxGridRowOdd";
					}*/
					console.log("----------------------");
					e.grid.updateRowStyles(e.rowIndex);
					if(dojo.query("input[type=checkbox]:checked", "grid_1").length>1){
			dojo.byId("edit_gd").disabled="true";
			dojo.byId("view_gd").disabled="true";
			dojo.byId("view_gd_css").className="button-gray";
			dojo.byId("edit_gd_css").className="button-gray";
		}else if(dojo.query("input[type=checkbox]:checked", "grid_1").length==1){
			dojo.forEach(
    			dojo.query("input[type=button]", "bt_gd"),
    			function(selectTag) {
        			selectTag.disabled = false;
    			}
			);
			dojo.forEach(
    			dojo.query("div", "bt_gd"),
    			function(selectTag) {
        			selectTag.className="button-blue";
    			}
			);
		}else{
			dojo.forEach(
    			dojo.query("input[type=button]", "bt_gd"),
    			function(selectTag) {
        			selectTag.disabled = true;
    			}
			);
			dojo.forEach(
    			dojo.query("div", "bt_gd"),
    			function(selectTag) {
        			selectTag.className="button-gray";
    			}
			);
		}
				}
			});	
	},
	
	initBrand:function(){
		var brandStr = this.brandStr;
		//brandStr = brandStr.substr(1,brandStr.length-2);
		brandArray = brandStr.split(',');
		var dlg=dijit.byId("dlg_brand");
		var node=dojo.byId("txt_brand");
		var selectedBrand=new Array();
		dojo.forEach(
    		dojo.query("input[type=checkbox]", "dlg_brand"),
    		function(selectTag){
    			if(brandStr.indexOf(selectTag.value) != -1){
    				dijit.byId(selectTag.id).setChecked(true);
					selectedBrand.push(selectTag.value);
    			}
        	}
		);
		var brandtxt="";
		if(selectedBrand.length>0){
			var i=0;
			for(i=0;i<selectedBrand.length-1;i++){
				brandtxt=brandtxt+selectedBrand[i]+", ";
			}
			brandtxt=brandtxt+selectedBrand[i];
		}
		dojo.byId("txt_brand").value=brandtxt;
		this.fun_readonlytest(brandArray);
	},
	fun_readonlytest:function(brandArray){
		dojo.forEach(
    		dojo.query("input[type=text]", "readonlytest"),
    		function(selectTag){
    			for(var i=0 ;i<brandArray.length;i++){
					items=brandArray[i].split('=');
					if(items[0].indexOf(selectTag.id) !=-1){
						selectTag.style.display="inline";
						if(items[1]==0){
							selectTag.disabled=true;
						}
					}
				}
        	}
		);
	},
	showBrandDlg:function(brandDlg,brandTxt){
		var dlg=dijit.byId(brandDlg);
		var node=dojo.byId(brandTxt);
		var selectedBrand=dojo.byId(brandTxt).value;
		dijit.popup.open({ around: node, popup: dlg }); 
		dojo.forEach(
    		dojo.query("input[type=checkbox]", brandDlg),
    		function(selectTag){
    			if(selectedBrand.indexOf(selectTag.value) != -1){
    				selectTag.checked=true;
    			}
        	}
		);	
	},
	hideBrandDlg:function(brandDlg,brandTxt){
		var selectedBrand=new Array();
		var inDlg=dijit.byId(brandDlg)
		dojo.forEach(
    		dojo.query("input[type=checkbox]", brandDlg),
    		function(selectTag){
        		if(selectTag.checked)
        			selectedBrand.push(selectTag.value);
        	}
		);
		dijit.popup.close(inDlg);
		var brandtxt="";
		var i=0;
		if(selectedBrand.length > 0){
			for(i=0;i<selectedBrand.length-1;i++){
				brandtxt=brandtxt+selectedBrand[i]+", ";
			}
			brandtxt=brandtxt+selectedBrand[i];
		}
		dojo.byId("txt_brand").value=brandtxt;
	},

	brandFilter: function(brandfilter, item){
		var ret = false;
		var brand=item.brand;
		if(typeof(brand)!= 'string' && brand != null){
			brand = brand[0];
		}
		
		var defaultFilterStatus = false;
		if((window.brands.indexOf(brand) >= 0 && brand != null)
		   || (window.brands.indexOf('Other') >= 0 && brand == null)){
				defaultFilterStatus = true;
		}
		if(defaultFilterStatus){
			if(brandfilter == window.brands){
				ret = true;
			}else if((brandfilter.indexOf(brand) >= 0 && brand != null)
			   || (brandfilter.indexOf('Other') >= 0 && brand == null)){
					ret = true;
			}
		}else{
			if ((brandfilter.indexOf(brand) >= 0 && activeId.indexOf(item.pid) >= 0 && brand != null) ||
			(brandfilter.indexOf('Other') >= 0 && activeId.indexOf(item.pid) >= 0 && brand == null)) {
				ret = true;
			}
		}
			
		return ret;
	}

}

